<?php
namespace api\user\controller;

use api\user\model\UserVerifyModel;
use api\common\controller\RestUserBaseController;
use think\Db;
class VerifyController extends RestUserBaseController
{
    protected $UserVerifyModel;

    public function __construct()
    {
        parent::__construct();
        $this->userId = $this->getUserId();
        $this->UserVerifyModel = new UserVerifyModel();
    }


    public function getuserverify()
    {

        $param          = $this->request->param();
        $param['where'] = [
            'user_id' => $this->userId
        ];
        $param['order'] = '-createtime';
        $verifyData = $this->UserVerifyModel->getDatas($param);
        $this->success('认证记录', $verifyData);
    }


    public function save()
    {
        $param = $this->request->param();
        
        $result = $this->validate($param, 'Verify');
        if ($result !== true) {
            $this->error($result);
        }
        $findUser = Db::name("user")->where('id', $this->userId)->find();
        if($findUser['verify_info']){
            $this->error("您已经通过实名验证了");
        }
        $findVerify = $this->UserVerifyModel->where(['user_id'=>$this->userId,'status'=>0])->count();
        if($findVerify>0){
            $this->error("您还有未审核的记录");
        }

        $finCardwhere['id_card'] = $param['id_card'];
        $finCardwhere['status'] = 2;
        $is_id_card = $this->UserVerifyModel->where($finCardwhere)->count('id');
        if($is_id_card){
            $this->error("身份证号已存在");
        }

        //组装数据
        $data = $this->_UserverifyObject($param);
        if (!$data) {
            $this->error('提交失败');
        }
        if ($this->UserVerifyModel->setUserverify($data)) {
            $this->success('提交成功');
        } else {
            $this->error('提交失败');
        }

    }

    public function edituserverify()
    {
        if ($this->request->isGet()) { 
            $param = $this->request->param();
            $verify = $this->UserVerifyModel->where(['id'=>$param['id']])->find();
            if($verify){
                $this->success('获取成功', $verify);
            }else{
               $this->error('获取失败'); 
            }
        }else{
            $param = $this->request->param();
            $result          = $this->validate($param, 'Verify');
            if ($result !== true) {
                $this->error($result);
            }
            //组装数据
            $data = $this->_UserverifyObject($param);
            if (!$data) {
                $this->error('修改失败');
            }
            $result = $this->UserVerifyModel->allowField(true)->data($data, true)->isUpdate(true)->save();
            if ($result) {
                $this->success('修改成功');
            } else {
                $this->error('修改失败');
            }  
        }

    }
    protected function _UserverifyObject($param)
    {
        $data['user_id']            = $this->userId;
        $data['true_name']          = $param['true_name'];
        $data['createtime']         = time();
        $data['id_card']            = $param['id_card'];
        // $data['id_card_hand_image']       = $param['id_card_hand_image'];
        // $data['id_card_front_image']      = $param['id_card_front_image'];
        // $data['id_card_behind_image']     = $param['id_card_behind_image'];
        if(isset($param['id'])){
          $data['id'] = $param['id'];
        }
        return $data;
    }


    public function unsetuserverify()
    {
        $param = $this->request->param();
        if (isset($param['id'])) {
                $count = $this->UserVerifyModel->where(['id' => $param['id'], 'user_id' => $this->userId])->count();
                if ($count == 0) {
                    $this->error('不存在,无法删除');
                }
                $result = $this->UserVerifyModel->where(['id' => $param['id']])->delete();
                if($result){
                     $this->success('删除成功');
                }else{
                      $this->error('删除失败'); 
                }
        } else{
            $this->error('参数错误');
        }
        
    }
}
